package cn.ad.server.web.filter.bid;

import cn.ad.server.web.spring.model.Exchange;
import cn.ad.server.web.spring.model.adview.AdViewBidRequest;
import cn.ad.server.web.spring.service.actions.context.BidContext;
import cn.ad.server.biz.internal.AdService;
import cn.ad.server.domain.ad.Strategy;
import cn.ad.server.domain.enumration.OS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author dongbin.yu
 * @from 2016-08-11
 * @since V1.0
 */
@Component
public class BidFloorAdViewMatcher implements BidMatcherChain {

	// @Value("${bid.adView.ratio}")
	// private double ratio;

	@Value("${bid.adView.ios}")
	private double iosPrice;

	@Value("${bid.adView.android}")
	private double androidPrice;

	@Autowired
	private AdService adService;

	@Override
	public boolean match(Strategy strategy, BidContext context) {
		// 订单id不在配置的列表内走比例为1的逻辑

		double ratio = adService.getRatio(strategy.getCampaign().getId());
		// AdView 出价特别逻辑
		if (context.getAdxType() == Integer.parseInt(Exchange.ADVIEW.getValue())) {
			if (context.getTransactionType() == AdViewBidRequest.AT.PRIORITY.ordinal()) {
				if (context.getOs() == OS.IOS.getValue()) {
					return (strategy.getUnitPrice().doubleValue() * ratio - iosPrice) > 0;
				} else if (context.getOs() == OS.ANDROID.getValue()) {
					return (strategy.getUnitPrice().doubleValue() * ratio - androidPrice) > 0;
				}
			}
		}

		return (strategy.getUnitPrice().doubleValue() * ratio - context.getBidFloor()) > 0;
	}

}
